<?php
    session_start();

    if (!isset($_SESSION['is_logged_in'])) {
        header("Location:../index.php");
        die();
    }
    
    ini_set('display_errors',"1");
    include("conexion.php");
    
    $operacion = $_POST["operacion"];
    $NumePart = $_POST["NumePart"];
    if ($operacion != "2") {
	    $NumeFech = $_POST["NumeFech"];
	    if ($operacion != "2") {
	    	$FechPart = new DateTime($_POST["FechPart"]);
	    	$FechPart = date_format($FechPart, "Y-m-d");
	    }
		$NumeTorneo = $_POST["NumeTorneo"];
		$NumeEquiLoca = $_POST["NumeEquiLoca"];
		$NumeEquiVisi = $_POST["NumeEquiVisi"];
		$PtosEquiLoca = $_POST["PtosEquiLoca"];
		$PtosEquiVisi = $_POST["PtosEquiVisi"];
		$PtosCuar1 = $_POST["PtosCuar1"];
		$PtosCuar2 = $_POST["PtosCuar2"];
		$PtosCuar3 = $_POST["PtosCuar3"];
		$PtosCuar4 = $_POST["PtosCuar4"];
    }
	
    $conn = new mysqli($dbhost, $dbuser, $dbpass, $db) or die("Problemas al conectar con la BD");
    
    switch ($operacion) {
        case 0:
            $strSQL = "SELECT COALESCE(MAX(NumePart), 0) + 1 NumePart FROM partidos";
            $tabla = $conn->query($strSQL);
            $fila = $tabla->fetch_array();
            $NumePart = $fila["NumePart"];
            
            $strSQL = "INSERT INTO partidos(NumePart, NumeTorneo, NumeFech, FechPart, NumeEquiLoca, NumeEquiVisi, PtosEquiLoca, PtosEquiVisi, PtosCuar1, PtosCuar2, PtosCuar3, PtosCuar4)";
            $strSQL.= " VALUES({$NumePart}, {$NumeTorneo}, {$NumeFech}, '{$FechPart}', {$NumeEquiLoca}, {$NumeEquiVisi}, {$PtosEquiLoca}, '{$PtosEquiVisi}', '{$PtosCuar1}', '{$PtosCuar2}', '{$PtosCuar3}', '{$PtosCuar4}')";

            if (!$conn->query($strSQL))
                echo "Fall&oacute; INSERT:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
            else
                echo "INSERT Correcto!";                
            
            $tabla->free();
            break;
        
        case 1:
            $strSQL = "UPDATE partidos";
            $strSQL.= " SET NumeTorneo = " . $NumeTorneo;
            $strSQL.= ", NumeFech = " . $NumeFech;
            $strSQL.= ", FechPart = '" . $FechPart . "'";
            $strSQL.= ", NumeEquiLoca = " . $NumeEquiLoca;
            $strSQL.= ", NumeEquiVisi = " . $NumeEquiVisi;
            $strSQL.= ", PtosEquiLoca = " . $PtosEquiLoca;
            $strSQL.= ", PtosEquiVisi = " . $PtosEquiVisi;
            $strSQL.= ", PtosCuar1 = '" . $PtosCuar1 . "'";
            $strSQL.= ", PtosCuar2 = '" . $PtosCuar2 . "'";
            $strSQL.= ", PtosCuar3 = '" . $PtosCuar3 . "'";
            $strSQL.= ", PtosCuar4 = '" . $PtosCuar4 . "'";
            $strSQL.= ", NumeEsta = 0";
            $strSQL.= " WHERE NumePart = " . $NumePart;
            
            if (!$conn->query($strSQL))
                echo "Fall&oacute; UPDATE:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
            else
                echo "UPDATE Correcto!";
            
            break;
            
        case 2:
        	$strSQL = "DELETE FROM partidosjugadores WHERE NumePart = " . $NumePart;
            if (!$conn->query($strSQL))
                echo "Fall&oacute; DELETE:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
            else
            	$strSQL = "DELETE FROM partidos WHERE NumePart = " . $NumePart;
            	if (!$conn->query($strSQL))
	                echo "Fall&oacute; DELETE:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
	            else
            		echo "DELETE Correcto!";
            
            break;
    }
    $conn->close();
?>